
namespace Behemoth.SharePoint.WebParts.ToolParts {

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    using Behemoth.SharePoint.WebParts;

  /// <summary>
  /// This tool part works only in MOSS and was completely ripped off from:
  /// http://darrenjohnstone.net/2008/01/23/sharepoint-picker-toolpart/
  /// </summary>
  /// <example>
  ///  // this property and method would go in the web part
  /// 
  ///  [Browsable(false), Category("Miscellaneous"), DefaultValue(""),WebPartStorage(Storage.Shared)]
  ///  public string TargetURL {
  ///    get; set;
  ///  }
  /// 
  ///  public override ToolPart[] GetToolParts() {
  ///    List&lt;ToolPart&gt; parts = new List&lt;ToolPart&gt;();
  ///    SiteAndListPickerToolPart picker = new SiteAndListPickerToolPart();
  ///
  ///    parts.AddRange(base.GetToolParts());
  ///
  ///    picker.Items.Add(new SiteAndListPickerItem("Site to query", "TargetURL", SiteAndListPickerType.Site));
  ///    picker.Title = "Select data source";
  ///    parts.Add(picker);
  ///
  ///    return parts.ToArray();
  ///  }
  /// </example>
  public class SiteAndListPickerToolPart : ToolPart, INamingContainer {

#if Vs2008      
    public List<PickerItem> Items { get; set; } 
#else
    List<SiteAndListPickerItem> _pickerItems;
    public List<SiteAndListPickerItem> Items {
      get { return _pickerItems; }
      set { _pickerItems = value; }
    }
#endif 
 
    /// <summary> 
    /// Initializes a new instance of the <see cref="SiteAndListPickerToolPart"/> class. 
    /// </summary> 
    public SiteAndListPickerToolPart() : base() {
      Items = new List<SiteAndListPickerItem>();
    }

    /// <summary> 
    /// Registers the script to load the web selector. 
    /// </summary> 
    void RegisterPopupScript() {
      StringBuilder sb = new StringBuilder();
      sb.Append("function cust_launchPickerSite(inputID)\r\n {\r\n  if (!document.getElementById) return;");
      sb.Append("var targetTextBox = document.getElementById(");
      sb.Append("inputID");
      sb.Append(");");
      sb.Append("if (targetTextBox == null) return;");
      sb.Append("var serverUrl = '");
      sb.Append(SPContext.Current.Web.ServerRelativeUrl);
      sb.Append("';var callbackSite = function(results){if (results == null || results[1] == null) return;targetTextBox.value = results[1];};\r\n");
      sb.Append("LaunchPickerTreeDialog('CbqPickerSelectSiteTitle','CbqPickerSelectSiteText','websOnly','',serverUrl, '','','','/_layouts/images/smt_icon.gif','', callbackSite);}");
      this.Page.ClientScript.RegisterClientScriptBlock(typeof(SiteAndListPickerToolPart), "PickerSite", sb.ToString(), true);

      sb = new StringBuilder();
      sb.Append("function cust_launchPickerList(inputID)\r\n {\r\n  if (!document.getElementById) return;");
      sb.Append("var targetTextBox = document.getElementById(");
      sb.Append("inputID");
      sb.Append(");");
      sb.Append("if (targetTextBox == null) return;");
      sb.Append("var serverUrl = '");
      sb.Append(SPContext.Current.Web.ServerRelativeUrl);
      sb.Append("';var callbackList = function(results){if (results == null || results[1] == null || results[2] == null) return;targetTextBox.value = results[1]+(results[1]=='/' ? '' : '/')+results[2];};\r\n");
      sb.Append("LaunchPickerTreeDialog('CbqPickerSelectListTitle','CbqPickerSelectListText','listsOnly','',serverUrl, '','','','/_layouts/images/smt_icon.gif','', callbackList);}");
      this.Page.ClientScript.RegisterClientScriptBlock(typeof(SiteAndListPickerToolPart), "PickerList", sb.ToString(), true);

      this.Page.ClientScript.RegisterClientScriptInclude("PickerTreeDialog", "/_layouts/1033/PickerTreeDialog.js");
    }

    /// <summary> 
    /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event. 
    /// </summary> 
    /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param> 
    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);
      RegisterPopupScript();
    }

    /// <summary> 
    /// Sends the tool part content to the specified HtmlTextWriter object, which writes the content to be rendered on the client. 
    /// </summary> 
    /// <param name="output">The HtmlTextWriter object that receives the tool part content.</param> 
    protected override void RenderToolPart(HtmlTextWriter output) {
      WebPart parent;
      int i = 0;
      parent = this.ParentToolPane.SelectedWebPart;
      if (Items.Count > 0) {
        output.Write("<table cellspacing=\"0\" border=\"0\" style=\"border-width:0px;width:100%;border-collapse:collapse;\">");
        foreach (SiteAndListPickerItem pi in Items) {
          output.Write("<tr><td>");
          output.Write("<div class=\"UserSectionHead\">");
          output.Write(pi.Title);
          output.Write("</div>");
          output.Write("<div class=\"UserControlGroup\"><nobr>");
          output.Write("<input type=\"text\" ");
          output.Write("value=\"" + WebPartUtilities.GetWebPartProperty(parent, pi.PropertyName) + "\"");
          output.Write(" name=\"picker_" + i.ToString() + "\" id=\"picker_" + i.ToString() + "\"/>");
          output.Write("<input type=\"button\" onclick=\"" + (pi.PickerType == SiteAndListPickerType.Site ? "cust_launchPickerSite" : "cust_launchPickerList"));
          output.Write("('picker_" + i.ToString() + "'); return false;\" value=\"...\"/>");
          output.Write("</nobr></div>");
          output.Write("</td></tr>");
          i++;
        }
        output.Write("</table>");
      }
    }

    /// <summary> 
    /// Called when the user clicks the OK or the Apply button in the tool pane. 
    /// </summary> 
    public override void ApplyChanges() {
      WebPart parent;
      int i = 0;
      parent = ParentToolPane.SelectedWebPart;
      if (Items.Count > 0) {
        foreach (SiteAndListPickerItem pi in Items) {
          WebPartUtilities.SetWebPartProperty(parent, pi.PropertyName, Page.Request.Form["picker_" + i.ToString()]);
          i++;
        }
      }
    }

  } // class

  public enum SiteAndListPickerType {
    Site,
    List
  }

  public class SiteAndListPickerItem {

#if Vs2008      
    public string Title { private set;  get;  }
    public string PropertyName { private set; get; }
    public PickerType PickerType { private set; get; } 
#else
    public string Title = string.Empty;
    public string PropertyName = string.Empty;
    public SiteAndListPickerType PickerType;
#endif

    public SiteAndListPickerItem(string title, string propertyName, SiteAndListPickerType pickerType) {
      Title = title;
      PropertyName = propertyName;
      PickerType = pickerType;
    }

  } // PickerItem

} // namespace
